package com.baymax.test.scene.demo

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Base64
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newScrollContainer
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream


/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       ShareScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/11/14 11:14
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class ShareScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?) =
        p1.newScrollContainer {
            addButton("clip") {
                val f = kotlin.run {
                    File("/sdcard/1.png").apply {
                        delete()
                    }
                }
                launch {
//                    withContext(Dispatchers.IO) {
//                        val capturePic = InstallClient.getInstance(requireSceneContext())
//                            .capturePic(f.absolutePath)
//                        Log.d(TAG, "capturePic: $capturePic")
//                    }
                }
            }

            addButton("query") {
                launch(Dispatchers.IO) {
//                    val bid = InstallClient.getInstance(requireSceneContext())
//                        .getSystemProperties("ro.build.id")
//                    Log.d(TAG, "capturePic: $bid")
//                    withContext(Dispatchers.Main) {
//                        Toast.makeText(requireSceneContext(), "id=$bid", Toast.LENGTH_SHORT).show()
//                    }
                }
            }
            addButton("clear gitv") {
                launch(Dispatchers.IO) {
//                    val bid = InstallClient.getInstance(requireSceneContext()).clearApk("com.dangbei.dbmusic")
//                    Log.d(TAG, "capturePic: $bid")
//                    withContext(Dispatchers.Main) {
//                        Toast.makeText(requireSceneContext(), "clear=$bid", Toast.LENGTH_SHORT).show()
//                    }
                }
            }

            addButton("load") {
                launch(Dispatchers.IO) {
                    val c = requireSceneContext()
                    val f = File(c.externalCacheDir ,"1.png")

                    //根据图片的宽高和屏幕的宽高计算采样率
                    val options = BitmapFactory.Options()
                    options.inJustDecodeBounds = true
                    BitmapFactory.decodeFile(f.absolutePath ,options)
                    val xScale: Int = scaleSize(
                        options.outWidth,
                        options.outWidth / 2
                    )
                    val yScale: Int = scaleSize(
                        options.outHeight,
                        options.outWidth / 2
                    )

                    val sampleSize = Math.max(xScale, yScale)

                    //设置采样率

                    //设置采样率
                    options.inJustDecodeBounds = false
                    options.inSampleSize = sampleSize
                    //解码文件
                    //解码文件
                    val bitmap = BitmapFactory.decodeFile(f.absolutePath, options)
                    val outputStream = FileOutputStream(File(c.externalCacheDir ,"1-1.png"))
                    //把bitmap100%高质量压缩 到 output对象里
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 75, outputStream)
                    outputStream.flush()
                    outputStream.close()
                }
            }

            addButton("json"){

                val j = JsonObject().apply {
                    addProperty("programSeriesId", "1200663377")
                }
                Log.d(TAG, "json:$j ")
                val tree = JsonParser().parse(j.toString()).asJsonObject
                Log.d(TAG, "tree:${tree.javaClass}  ,${tree.asJsonObject}")

            }


            //  AspShareManager.getInstance().shareMedia(requireSceneContext(), "", "")
        }

    private fun scaleSize(origin: Int, dest: Int): Int {
        if (dest == 0 || origin == 0) {
            return 1
        }
        return if (origin % dest == 0) origin / dest else origin / dest + 1
    }
}
